package icbc.nav.control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import icbc.app.AppGlobal;
	
	import org.osflash.signals.Signal;
	
	public class TooltipImageControl extends Sprite
	{
		public static var EVENT_CLICK:String = "TIP_EVENT_CLICK";
		private static const TXT_CHILD_NAME:String = "txt";
		private static const FONT_SIZE:uint = 16;
		private static const FONT_COLOR:uint = 0xE381B2;
		private static const TEXT_HEIGHT:uint = 27;
		
		[Embed (source="/assets/tip_close.png")]
		public var tipCloseImage:Class;
		
		[Embed (source="/assets/tip_close_over.png")]
		public var tipCloseOverImage:Class;

		protected var panel:Sprite;
		public var viewWidth:uint;
		public var viewHeight:uint;
		
		private var gap:uint = 5;
		public var data:Object;
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var closeImage:Sprite;
		private var iconOverImage:Sprite;
		private var tipImage:Sprite;
		private var rounded:uint = 20;
		
		private var place:Sprite;
		private var scrollPaneWidth:uint = 290;
		private var scrollPaneHeight:uint = 440;
		public var onReady:Signal = new Signal();
		
		public function TooltipImageControl(data:Object, vWidth:uint = 300, vHeight:uint = 250)
		{
			super();
			this.data = data;
			viewWidth = vWidth;
			viewHeight = vHeight;
			scrollPaneWidth = viewWidth - 10;
			scrollPaneHeight = viewHeight - 10;
			this.controlWidth = vWidth;
			this.controlHeight = vHeight;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(Event.RESIZE, initMain);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function initMain(evt:Event = null):void
		{
			if(stage.stageWidth>0)
				stage.removeEventListener(Event.RESIZE, initMain);
			else return;
			initContent();
		}
		
		private function initContent():void
		{
			panel = new Sprite();
			addChild(panel);
			
			loadImage();
		}
		
		private function buildCloseIcon():void
		{
			closeImage = new Sprite();

			var iconClose:Bitmap = new tipCloseImage();
			closeImage.addChild(iconClose);
			
			iconOverImage = new Sprite();
			var iconOverClose:Bitmap = new tipCloseOverImage();
			iconOverImage.addChild(iconOverClose);
			iconOverImage.alpha = 0;

			closeImage.addChild(iconOverImage);
			
			panel.addChild(closeImage);
			var gapx:uint = 25;
			var gapy:uint = 3;
			closeImage.x = panel.width - gapx;
			closeImage.y = gapy;
			addIconListeners();
		}
		
		private function setBasePanel(base:Sprite):void
		{
			base.graphics.clear();
			base.graphics.beginFill(0x999999);
			base.graphics.drawRect(0, 0, viewWidth, viewHeight);
			base.graphics.endFill();
		}
		
		private function loadImage():void
		{
			if(data.image == null)
				return;
			tipImage = new Sprite();
			panel.addChild(tipImage);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, tipImageComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, tipImageOnIOError);
			loader.load(new URLRequest(AppGlobal.combinePath(data.image)));
			tipImage.addChild(loader);
			tipImage.y = 15;
		}
		private function tipImageOnIOError(evt:IOErrorEvent):void
		{
			trace("tooltipImageControl.Image["+ data.image +"].IOError: "+ evt.text);
		}		
		
		private function tipImageComplete(evt:Event):void
		{
			var image:DisplayObject = (evt.target as LoaderInfo).content;
			viewWidth = image.width + 4;
			viewHeight = image.height + 22;
			setPanel();
			onReady.dispatch();
		}
		
		private function setPanel():void
		{
			setBasePanel(panel);
			tipImage.x = 2;
			tipImage.y = 20;
			buildCloseIcon();
		}
		
		private function closeHandler(evt:MouseEvent = null):void
		{
			removeIconListeners();
			parent.removeChild(this);
		}
		
		public function dispose():void
		{
			closeHandler();
		}

		private function addIconListeners():void
		{
			closeImage.addEventListener(MouseEvent.CLICK, closeHandler);
			iconOverImage.addEventListener(MouseEvent.MOUSE_OVER, onIconRollOver);
			iconOverImage.addEventListener(MouseEvent.MOUSE_OUT, onIconRollOut);
		}
		
		private function removeIconListeners():void
		{
			closeImage.removeEventListener(MouseEvent.CLICK, closeHandler);
			iconOverImage.removeEventListener(MouseEvent.MOUSE_OVER, onIconRollOver);
			iconOverImage.removeEventListener(MouseEvent.MOUSE_OUT, onIconRollOut);
		}

		private function onIconRollOver(evt:Event):void
		{
			trace("->over·");
			var tween:TweenLite = new TweenLite(iconOverImage,1,{ease:Strong.easeOut,alpha:1});
		}
		
		private function onIconRollOut(evt:Event):void
		{
			trace("->out·");
			var tween:TweenLite = new TweenLite(iconOverImage,2,{ease:Strong.easeOut,alpha:0});
		}
	}
}